package com.tbit.main.pojo.maintain_performance.stat;

import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;

/**
 * 运维绩效图表数据
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/2/25
 */
@Data
public class PerformanceChartResp {
    /**
     * 每日对应的数据（直方图）
     */
    private List<PerformanceChartPerDateDetail> details;
    /**
     * 每个运维人员对应的有效次数合计（饼图）
     */
    private List<PerformancePieChartData<Integer>> validCount;
    /**
     * 每个运维人员对应的绩效合计（饼图）
     */
    private List<PerformancePieChartData<BigDecimal>> performanceSum;

    public static PerformanceChartResp fromDo(List<PerformanceChartDo> chartDos) {
        Map<LocalDate, List<PerformancePieChartData<Integer>>> date2ValidCountMap = new HashMap<>();
        Map<LocalDate, List<PerformancePieChartData<BigDecimal>>> date2PerfSumMap = new HashMap<>();
        Map<Integer, PerformancePieChartData<Integer>> user2ValidCountMap = new HashMap<>();
        Map<Integer, PerformancePieChartData<BigDecimal>> user2PerfSumMap = new HashMap<>();
        for (PerformanceChartDo chartDo : chartDos) {
            LocalDate date = chartDo.getDate();

            // 当天有效次数
            PerformancePieChartData<Integer> validCountData = new PerformancePieChartData<>();
            validCountData.setAccountUserName(chartDo.getAccountUserName());
            validCountData.setData(chartDo.getValidCount());
            date2ValidCountMap.computeIfAbsent(date, k -> new ArrayList<>())
                    .add(validCountData);

            // 当天绩效总和
            PerformancePieChartData<BigDecimal> perfSumData = new PerformancePieChartData<>();
            perfSumData.setAccountUserName(chartDo.getAccountUserName());
            perfSumData.setData(chartDo.getPerformanceSum());
            date2PerfSumMap.computeIfAbsent(date, k -> new ArrayList<>())
                    .add(perfSumData);

            // 用户有效次数总和
            PerformancePieChartData<Integer> userValidCountData = user2ValidCountMap.computeIfAbsent(chartDo.getAccountUserId(), k -> {
                PerformancePieChartData<Integer> data = new PerformancePieChartData<>();
                data.setAccountUserId(chartDo.getAccountUserId());
                data.setAccountUserName(chartDo.getAccountUserName());
                data.setData(0);
                return data;
            });
            userValidCountData.setData(userValidCountData.getData() + chartDo.getValidCount());

            // 用户绩效总和
            PerformancePieChartData<BigDecimal> userPerfSumData = user2PerfSumMap.computeIfAbsent(chartDo.getAccountUserId(), k -> {
                PerformancePieChartData<BigDecimal> data = new PerformancePieChartData<>();
                data.setAccountUserId(chartDo.getAccountUserId());
                data.setAccountUserName(chartDo.getAccountUserName());
                data.setData(BigDecimal.ZERO);
                return data;
            });
            userPerfSumData.setData(userPerfSumData.getData().add(chartDo.getPerformanceSum()));
        }

        // 封装返回结果
        PerformanceChartResp resp = new PerformanceChartResp();
        resp.setDetails(new ArrayList<>());
        for (LocalDate date : date2ValidCountMap.keySet()) {
            PerformanceChartPerDateDetail detail = new PerformanceChartPerDateDetail();
            detail.setDate(date);
            detail.setValidCount(date2ValidCountMap.get(date));
            detail.setPerformanceSum(date2PerfSumMap.get(date));
            resp.getDetails().add(detail);
        }
        // 日期倒序排序
        resp.getDetails().sort(Comparator.comparing(PerformanceChartPerDateDetail::getDate).reversed());

        resp.setValidCount(new ArrayList<>());
        resp.setPerformanceSum(new ArrayList<>());
        user2ValidCountMap.keySet().stream()
                .sorted()
                .forEachOrdered(userId -> {
                    resp.getValidCount().add(user2ValidCountMap.get(userId));
                    resp.getPerformanceSum().add(user2PerfSumMap.get(userId));
                });
        return resp;
    }
}
